/** 
 * This file is part of osami-fin-demo project( http://code.google.com/p/osami-fin-demo/ )
 * @license Apache License 2.0
 */
package org.osami.appevent;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.TelephonyManager;
import android.util.Log;

/** Miscellaneous utilities */
class LocalUtilities {

	public static TelephonyManager getTelephony(Context aContext) {
		Logger log = new Logger("getDisplayNameForPhoneNumber");

		TelephonyManager phone = (TelephonyManager) aContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (phone == null) {
			log.e("Failed to get TelephonyManager");
			return null;
		}
		return phone;
	}

	public static String getDisplayNameForPhoneNumber(Context aContext,
			String aPhoneNumber) {
		Logger log = new Logger("getDisplayNameForPhoneNumber");
		
		if (aPhoneNumber == null) {
			log.e("aPhoneNumber is null");
			return "";
		}
		if (aPhoneNumber.length() == 0) {
			log.e("aPhoneNumber is empty");
			return "";
		}
		log.d("Resolving name for", aPhoneNumber);
		Uri uri = null;
		try {
			uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri
				.encode(aPhoneNumber));
		} catch (Exception e) {
			/* Nothing; not enough permissions*/
			log.d("Not enough permissions for PhoneLookup");
		}
		
		Cursor cursor = null;
		try {
			log.d("Attempting to access PhoneLookup");
			cursor = aContext.getContentResolver().query(uri,
				new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
			log.d("Got PhoneLookup successfully");
			// Assuming there is only 1 contact for phone number if any
			if (cursor.moveToFirst()) {
				String displayName = cursor.getString(cursor
						.getColumnIndex(PhoneLookup.DISPLAY_NAME));
				log.df("Got displayname:%s for %s", displayName, aPhoneNumber);
				return displayName;
			}
		}
		catch (Exception e) {
			log.d("Query failed");
		} finally {
			if(cursor != null) {
				cursor.close();
			}
		}

		return "";
	}
}
/**
 * Android Logging utility for having tag preset to easily filter in Eclipse's
 * LogCat view. view.
 */
class Logger {
	String tag = "org.osami";
	String prefix = "";

	public Logger(String aPrefix) {
		this.prefix = aPrefix;
	}

	protected void log(int aPriority, String aMsg) {
		Log.println(aPriority, tag, aMsg);
	}

	protected void log(int aPriority, Object... aArgs) {
		String msg = "";
		if(prefix.length() > 0){
			msg = prefix + ">";
		}
		
		for (int i = 0; i < aArgs.length; i++) {
			msg = msg.concat(aArgs[i].toString());
			if (i < aArgs.length-1) {
				msg = msg.concat(" ");
			}
		}
		
		log(aPriority, msg);
	}

	protected void logf(int aPriority, String aFormat, Object... aArgs) {
		log(aPriority, String.format(aFormat, aArgs));
	}

	// Public APIs ================================
	public void d(Object... aArgs) {
		log(Log.DEBUG, aArgs);
	}

	public void df(String aFormat, Object... aArgs) {
		logf(Log.DEBUG, aFormat, aArgs);
	}
	
	public void w(Object... aArgs) {
		log(Log.WARN, aArgs);
	}

	public void wf(String aFormat, Object... aArgs) {
		logf(Log.WARN, aFormat, aArgs);
	}
	
	public void e(Object... aArgs) {
		log(Log.ERROR, aArgs);
	}

	public void ef(String aFormat, Object... aArgs) {
		logf(Log.ERROR, aFormat, aArgs);
	}

	
}
